<template>
  <div class="desktop">
    <div class="desktop-main">
      <div class="bar">
        <div class="singersex1" id="main1"></div>
        <div class="singersex1" id="main2"></div>
        <div class="singersex" id="main3"></div>
        <div class="singersex" id="main4"></div>
      </div>
      <div class="line">
        <div class="linemi" id="main5"></div>
        <div class="linemi" id="main6"></div>
      </div>
    </div>
    <div class="author-title">
      <p>基于Springboot+uniapp+vue的云音乐播放平台</p>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import axios from "axios";

export default {
  name: "index",
  data() {
    return {
      singerSex: [],
      userSex: [],
      listStyle: [],
      songStyle: [],
      singerHot: {
        x: [],
        y: []
      },
      hotSong:{
        x:[],
        y:[]
      }
    }
  },
  methods: {
    getSingerSex() {
      axios.get("/singer/findSingerSex").then(res => {
        this.singerSex = res.data.data
        var chartDom = document.getElementById('main1');
        var myChart = echarts.init(chartDom);
        let option = {
          tooltip: {
            trigger: 'item'
          },
          legend: {
            top: '7%',
            left: 'left'
          },
          title: {
            text: "歌手信息比例"
          },
          series: [
            {
              name: 'Access From',
              type: 'pie',
              radius: ['40%', '70%'],
              avoidLabelOverlap: false,
              itemStyle: {
                borderRadius: 10,
                borderColor: '#fff',
                borderWidth: 2
              },
              label: {
                show: false,
                position: 'center'
              },
              emphasis: {
                label: {
                  show: true,
                  fontSize: 40,
                  fontWeight: 'bold'
                }
              },
              labelLine: {
                show: false
              },
              data: this.singerSex
            }
          ]
        };
        myChart.setOption(option);
      })
    },
    getUserSex() {
      axios.get("/user/findUserSex").then(res => {
        this.userSex = res.data.data
        var chartDom = document.getElementById('main2');
        var myChart = echarts.init(chartDom);
        let option = {
          tooltip: {
            trigger: 'item'
          },
          legend: {
            top: '7%',
            left: 'left'
          },
          title: {
            text: "用户信息比例"
          },
          series: [
            {
              name: 'Access From',
              type: 'pie',
              radius: ['40%', '70%'],
              avoidLabelOverlap: false,
              itemStyle: {
                borderRadius: 10,
                borderColor: '#fff',
                borderWidth: 2
              },
              label: {
                show: false,
                position: 'center'
              },
              emphasis: {
                label: {
                  show: true,
                  fontSize: 40,
                  fontWeight: 'bold'
                }
              },
              labelLine: {
                show: false
              },
              data: this.userSex
            }
          ]
        };
        myChart.setOption(option);
      })
    },
    getListStyle() {
      axios.get("/list/findListStyle").then(res => {
        this.listStyle = res.data.data
        var chartDom = document.getElementById('main3');
        var myChart = echarts.init(chartDom);
        let option = {
          tooltip: {
            trigger: 'item'
          },
          legend: {
            top: '7%',
            left: 'center'
          },
          title: {
            text: "歌单风格比例"
          },
          series: [
            {
              name: 'Access From',
              type: 'pie',
              radius: ['40%', '70%'],
              avoidLabelOverlap: false,
              itemStyle: {
                borderRadius: 10,
                borderColor: '#fff',
                borderWidth: 2
              },
              label: {
                show: false,
                position: 'center'
              },
              emphasis: {
                label: {
                  show: true,
                  fontSize: 40,
                  fontWeight: 'bold'
                }
              },
              labelLine: {
                show: false
              },
              data: this.listStyle
            }
          ]
        };
        myChart.setOption(option);
      })
    },
    getSongStyle() {
      axios.get("/song/findSongStyle").then(res => {
        this.songStyle = res.data.data
        var chartDom = document.getElementById('main4');
        var myChart = echarts.init(chartDom);
        let option = {
          tooltip: {
            trigger: 'item'
          },
          legend: {
            top: '7%',
            left: 'center'
          },
          title: {
            text: "歌曲风格比例"
          },
          series: [
            {
              name: 'Access From',
              type: 'pie',
              radius: ['40%', '70%'],
              avoidLabelOverlap: false,
              itemStyle: {
                borderRadius: 10,
                borderColor: '#fff',
                borderWidth: 2
              },
              label: {
                show: false,
                position: 'center'
              },
              emphasis: {
                label: {
                  show: true,
                  fontSize: 30,
                  fontWeight: 'bold'
                }
              },
              labelLine: {
                show: false
              },
              data: this.songStyle
            }
          ]
        };
        myChart.setOption(option);
      })
    },
    getSingerHot() {
      axios.get("/song/findSingerHot").then(res => {
        for (let i = 0; i < res.data.data.length; i++) {
          if (res.data.data[i].name.length > 3) {
            this.singerHot.x.push(res.data.data[i].name.substring(0, 3))
          } else {
            this.singerHot.x.push(res.data.data[i].name)
          }
          this.singerHot.y.push(res.data.data[i].value)
        }
        var chartDom = document.getElementById('main5');
        var myChart = echarts.init(chartDom);
        var option;

        option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          title: {
            text: "歌手歌曲热度播放榜"
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: this.singerHot.x,
              axisTick: {
                alignWithLabel: true
              }
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: 'Direct',
              type: 'bar',
              barWidth: '60%',
              data: this.singerHot.y
            }
          ]
        };

        option && myChart.setOption(option);
      })
    },
    getHotSong(){
      axios.get("/song/findHotSong").then(res=>{
        console.log(res)
        for (let i =0 ;i<res.data.data.length;i++){
          this.hotSong.x.push(res.data.data[i].name)
          this.hotSong.y.push(res.data.data[i].value)
        }
        var chartDom = document.getElementById('main6');
        var myChart = echarts.init(chartDom);
        var option;

        option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          title: {
            text: "歌曲热度播放榜"
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: this.hotSong.x,
              axisTick: {
                alignWithLabel: true
              }
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: 'Direct',
              type: 'bar',
              barWidth: '60%',
              data: this.hotSong.y
            }
          ]
        };

        option && myChart.setOption(option);
      })
    }
  },
  created() {
    this.getSingerSex()
    this.getUserSex()
    this.getListStyle()
    this.getSongStyle()
    this.getSingerHot()
    this.getHotSong()
  }
}
</script>

<style>
.desktop {
  width: 100%;
  height: 100%;
  position: relative;
}

.desktop-main {
  position: absolute;
  width: 99%;
  height: 80%;
  transform: translate(-50%, -50%);
  left: 50%;
  top: 40%;
  background-size: 100% 100%;
  display: flex;
}

.author-title {
  width: 100%;
  height: 60px;
  background-color: #008390;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 100%;
  left: 50%;
  text-align: center;
  font-size: 13px;
  color: #fff;
}

.singersex {
  width: 50%;
  height: 50%;
  display: inline-block;
}

.singersex1 {
  width: 50%;
  height: 50%;
  display: inline-block;
}

.bar {
  width: 50%;
  height: 120%;
  display: inline-block;
}

.line {
  width: 50%;
  height: 120%;
  display: inline-block;
}

.linemi {
  width: 100%;
  height: 50%;
  display: inline-block;
}
</style>